<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>网站用户名验证</title>
</head>
<body>
    <input type="text" name="username">
    <span></span>
    <script>
        document.querySelector('[name="username"]').addEventListener('keyup',e=>{
            let value = e.target.value;
            let reg = /^[a-z][\w-]{4,7}$/i;
            document.querySelector("span").innerHTML = reg.test(value) ? "正确" : "错误";
        })
    </script>
</body>
</html>